package kr.saeildamil.androidStudy_second.multimedia;

import java.io.IOException;

import kr.saeildamil.androidStudy_second.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ChangeVolume extends Activity {
	MediaPlayer mPlayer;
	AudioManager mAm;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_changevolume);
		mPlayer = new MediaPlayer();
		String sd = Environment.getExternalStorageDirectory().getAbsolutePath();
		try {
			mPlayer.setDataSource(sd + "/dingdong.mp3");
			mPlayer.prepare();
		} catch (Exception e) {
			e.printStackTrace();
		}

		mAm = (AudioManager) getSystemService(AUDIO_SERVICE);
		SeekBar seek = (SeekBar) findViewById(R.id.seekvolume);
		seek.setMax(mAm.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
		seek.setProgress(mAm.getStreamVolume(AudioManager.STREAM_MUSIC));
		seek.setOnSeekBarChangeListener(mOnSeek);
	}

	OnSeekBarChangeListener mOnSeek = new OnSeekBarChangeListener() {
		public void onStopTrackingTouch(SeekBar arg0) {
		}

		public void onStartTrackingTouch(SeekBar arg0) {
		}

		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			mAm.setStreamVolume(AudioManager.STREAM_MUSIC, progress,
					AudioManager.FLAG_SHOW_UI);
		}
	};

	protected void onPause() {
		super.onPause();
		if (mPlayer != null) {
			mPlayer.release();
			mPlayer = null;
		}
	}

	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.btnplay:
			if (mPlayer.isPlaying()) {
				mPlayer.pause();
			} else {
				mPlayer.start();
			}
			break;

		case R.id.btnvoldown:
			mAm.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI
							| AudioManager.FLAG_ALLOW_RINGER_MODES
							| AudioManager.FLAG_PLAY_SOUND
							| AudioManager.FLAG_VIBRATE);
			break;
		case R.id.btnvolup:
			mAm.adjustStreamVolume(AudioManager.STREAM_MUSIC,
					AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI
							| AudioManager.FLAG_ALLOW_RINGER_MODES
							| AudioManager.FLAG_PLAY_SOUND
							| AudioManager.FLAG_VIBRATE);
			break;
		case R.id.btnring:
			switch (mAm.getRingerMode()) {
			case AudioManager.RINGER_MODE_NORMAL:
				mAm.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				break;
			case AudioManager.RINGER_MODE_SILENT:
				mAm.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
				break;
			case AudioManager.RINGER_MODE_VIBRATE:
				mAm.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				break;
			}
			break;
		case R.id.btneffect1:
			mAm.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
			break;
		case R.id.btneffect2:
			mAm.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
			break;
		case R.id.btneffect3:
			mAm.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
			break;
		}
	};
}
